<?php
/**
 * 需求调拨申请系统自动审核规则模型
 * 自动审核规则主要用于系统是否可以自动审核调拨需求判断
 */
class Backup_Model_TransferApplyAutoCheckRule extends Model_Base {

	public function __construct() {
		$this->db = $this->db();
	}

	public function getId($transferFromWarehouse, $transferToWarehouse) {
		$where = "";
		$where .= $this->db->quoteInto("from_warehouse=?", $transferFromWarehouse);
		$where .= " AND " . $this->db->quoteInto("to_warehouse=?", $transferToWarehouse);
		return $this->db->fetchOne('select id from b_transfer_apply_auto_check_rule  where ' . $where);
	}

	/**
	 * 根据调出仓和调入仓获取系统自动审核规则
	 * @param  [type] $transferFromWarehouse 调出仓
	 * @param  [type] $transferToWarehouse   调入仓
	 * @return [type]                        [description]
	 */
	public function getOneDetail($transferFromWarehouse, $transferToWarehouse) {
		//为什么要放在静态变量？因为每一条调拨申请都需要检查是否符合系统自动审核规则，
		//从而需要循环获取审核规则，为了避免重复查询数据库，所以这样
		$cacheKey = md5($transferFromWarehouse) . md5($transferToWarehouse);
		static $allRule = [];
		if (isset($allRule[$cacheKey])) {
			return $allRule[$cacheKey];
		}

		$where = "";
		$where .= $this->db->quoteInto("rule.from_warehouse=?", $transferFromWarehouse);
		$where .= " AND " . $this->db->quoteInto("rule.to_warehouse=?", $transferToWarehouse);
		$warehouseRule = $this->db->fetchAll('select rule_item.*,rule.* from b_transfer_apply_auto_check_rule as rule left join b_transfer_apply_auto_check_rule_item as rule_item on rule_item.auto_check_rule_id=rule.id where ' . $where);
		if ($warehouseRule) {
			$allRule[$cacheKey] = $warehouseRule;
		}

		return $warehouseRule;
	}

	/**
	 * 获取所有的系统自动审核规则
	 * @return [type] [description]
	 */
	public function getAll() {
		return $this->db->fetchAll('select rule_item.*,rule.* from b_transfer_apply_auto_check_rule as rule left join b_transfer_apply_auto_check_rule_item as rule_item on rule_item.auto_check_rule_id=rule.id ');
	}

	/**
	 * 保存系统自动审核调拨申请规则
	 * @param  [type] $transferFromWarehouse [description]
	 * @param  [type] $transferToWarehouse   [description]
	 * @param  array  $ruleItem              [description]
	 * @return [type]                        [description]
	 */
	public function save($transferFromWarehouse, $transferToWarehouse, array $ruleItem) {
		$this->writeDb = $this->db('write_db');
		$ruleId = $this->getId($transferFromWarehouse, $transferToWarehouse);
		$this->writeDb->beginTransaction();
		try {
			if ($ruleId) {
				$this->writeDb->delete('b_transfer_apply_auto_check_rule_item', $this->writeDb->quoteInto("auto_check_rule_id=?", $ruleId));
			} else {
				$ruleData = [
					'from_warehouse' => $transferFromWarehouse,
					'to_warehouse' => $transferToWarehouse,
				];
				$this->writeDb->insert('b_transfer_apply_auto_check_rule', $ruleData);
				$ruleId = $this->writeDb->lastInsertId();
			}
			foreach ($ruleItem as &$oneRuleItem) {
				$oneRuleItem['auto_check_rule_id'] = $ruleId;
			}
			unset($oneRuleItem);

			$this->insertBatch($this->writeDb, 'b_transfer_apply_auto_check_rule_item', $ruleItem);

			$this->writeDb->commit();
			return true;
		} catch (Exception $e) {
			$this->writeDb->rollBack();
			return $this->_error($e->getMessage());
		}

	}
}